< previous page page_184 next page >

Page 184
note.gif If you are working on converting programs from an older version of Visual Basic, you might find strings combined using the plus sign operator (+). This usage was prevalent in earlier versions of Visual Basic, as well as in older BASIC languages. Although Visual Basic still supports using the plus sign operator to concatenate strings (in case this operator is present in older code that you are modifying), I recommend that you use the ampersand for this purpose. This operator helps to avoid confusion with the mathematical addition operation. square.gif

Listing 9.2 shows how to concatenate strings in a simple program to generate an address label. The fields from the different text boxes are combined to create the different lines of the address label.
Listing 9.2 MAILING.ZIPString Concatenation Used in Address Labels
Private Sub cmdPrintAddr_Click()
    Dim sFName As String, sLName As String
    Dim sAddr As String, sCity As String
    Dim sState As String, sZIP As String
    Dim sTitle As String
    Dim sName As String, sCSZ As String

    sFName = txtFirst.Text
    sLName = txtLast.Text
    sAddr = txtAddress.Text
    sCity = txtCity.Text
    sState = txtState.Text
    sZIP = txtZIP.Text
    If optMr.Value Then sTitle = "Mr. "
    If optMrs.Value Then sTitle = "Mrs. "
    If optMiss.Value Then sTitle = "Miss "
    If optMs.Value Then sTitle = "Ms. "
    sName = sTitle & sFName & " " & sLName
    sCSZ = sCity & ", " & sState & "  " & sZIP
    Printer.Print sName
    Printer.Print sAddr
    Printer.Print sCSZ

End Sub
The form for this program is shown in Figure 9.3.
0184-01.jpg
Figure 9.3
The address label application shows how
strings can be combined for display or printing.

 
< previous page page_184 next page >